<template>
  <div class="my-content">
    <div class="header user-info" v-if="user">
      <!-- //用户信息 -->
      <div class="base-info">
        <div class="left">
          <van-image class="pic" round fit="cover" :src="userInfo.photo" />
          <span class="name">{{userInfo.name}}</span>
        </div>
        <div class="right">
          <van-button to="/user/profile" round type="default" size="mini">编辑资料</van-button>
        </div>
      </div>

      <!-- 用户数据 -->
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{userInfo.art_count}}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.fans_count}}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.follow_count}}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.like_count}}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>
    <div class="header no-login" v-else>
      <div class="login-btn" @click="$router.push('/login')">
        <img class="user-img" src="~@/assets/logo.png" alt />
        <span class="text">登录/注册</span>
      </div>
    </div>

    <!-- 导航 -->
    <van-grid class="grid-nav kongge" :column-num="2" clickable>
      <van-grid-item class="grid-icon">
        <i slot="icon" class="iconfont icon-shoucang"></i>
        <span class="text" slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-icon">
        <i slot="icon" class="iconfont icon-lishi"></i>
        <span class="text" slot="text">历史</span>
      </van-grid-item>
    </van-grid>

    <!-- 单元格 -->
    <van-cell title="消息通知" is-link />
    <van-cell class="kongge" title="小智同学" is-link />
    <van-cell class="logout-cell" title="退出登录" v-if="user" @click="onLogout" />
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import { mapState } from 'vuex'
import { userinfoAPI } from '@/api/user'

export default {
  name: 'MyIndex',
  // import引入的组件需要注入到对象中才能使用
  components: {},
  props: {},
  data () {
    // 这里存放数据
    return {
      isLogin: false,
      userInfo: {}
    }
  },
  // 计算属性 类似于data概念
  computed: { ...mapState(['user']) },
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    onLogout () {
      // 退出提示
      this.$dialog.confirm({
        title: '提示',
        message: '确认退出吗'
      })
        .then(() => {
          // on confirm
          this.$store.commit('setUser', null)
          // vuex中setUser方法自动备份到本地存储
          // 确认退出：清除登录状态
        })
        .catch(() => {
          // on cancel
        })
    },
    async loadUserInfo () {
      try {
        const { data } = await userinfoAPI()
        this.userInfo = data.data
        // console.log(data)
      } catch (error) {
        this.$toast('获取数据失败')
      }
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created () {
    if (this.user) {
      this.loadUserInfo()
    }
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {

  },
  beforeCreate () { }, // 生命周期 - 创建之前
  beforeMount () { }, // 生命周期 - 挂载之前
  beforeUpdate () { }, // 生命周期 - 更新之前
  updated () { }, // 生命周期 - 更新之后
  beforeDestroy () { }, // 生命周期 - 销毁之前
  destroyed () { }, // 生命周期 - 销毁完成
  activated () { } // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style scoped lang='less'>
.my-content {
  background-color: rgb(243, 243, 243);
  height: 617 * 2px;
  .header {
    height: 361px;
    background: url('~@/assets/background.jpg');
    background-size: cover;
  }
  .no-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
    .user-img {
      width: 132px;
      height: 132px;
    }
    .text {
      font-size: 28px;
      color: rgb(75, 34, 59);
    }
  }
  .user-info {
    .base-info {
      height: 231px;
      width: 100%;

      padding: 76px 32px 23px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;

      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .pic {
          height: 132px;
          width: 132px;
          margin-right: 23px;
          border: 2px solid #fff;
        }
        .name {
          color: rgb(0, 0, 0);
          font-size: 30px;
        }
      }
    }
    .data-stats {
      height: 130px;

      display: flex;
      .data-item {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        color: rgb(0, 0, 0);
      }
      .count {
        font-size: 36px;
      }
      .text {
        font-size: 23px;
      }
    }
  }
  .grid-nav {
    .grid-icon {
      height: 141px;
      i.iconfont {
        font-size: 45px;
      }
      .icon-shoucang {
        color: #eb5253;
      }
      .icon-lishi {
        color: #ff9d1d;
      }
      span.text {
        font-size: 28px;
      }
    }
  }
  .logout-cell {
    text-align: center;
    align-items: center;

    color: red;
  }
  .kongge {
    margin-bottom: 9px;
  }
}
</style>
